package one;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class time extends JFrame{
	
	JLabel l1=new JLabel();
	
       time(){
		super("香港回归倒计时！");
		setSize(700,80);
		setLocationRelativeTo(null);
		setLayout(new FlowLayout());
		l1.setForeground(Color.BLACK);
		l1.setFont(new Font("楷体",Font.BOLD|Font.ITALIC|Font.CENTER_BASELINE,20));
		add(l1);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	private static long years=1997;
	private static long months=6;
	private static long dates =30;
	private static long hs=23;
	private static long ms=59;
	private static long ss=49;
	
	private static long day = 0;
	private static long hour = 0;
	private static long minute = 0;
	private static long second = 0;
 
	private static boolean dayNotAlready = false;
	private static boolean hourNotAlready = false;
	private static boolean minuteNotAlready = false;
	private static boolean secondNotAlready = false;
	

	/**
	 * 初始化赋值
	 * 
	 * @param totalSeconds
	 */
	private static void initData(long totalSeconds) {
		resetData();
		if (totalSeconds >= 0) {
			secondNotAlready = true;
			second = totalSeconds;
			if (second >= 60) {
				minuteNotAlready = true;
				minute = second / 60;
				second = second % 60;
				if (minute >= 60) {
					hourNotAlready = true;
					hour = minute / 60;
					minute = minute % 60;
					if (hour > 24) {
						dayNotAlready = true;
						day = hour / 24;
						hour = hour % 24;
					}
				}
			}
		}
	}
 
	private static void resetData() {
		day = 0;
		hour = 0;
		minute = 0;
		second = 0;
		dayNotAlready = false;
		hourNotAlready = false;
		minuteNotAlready = false;
		secondNotAlready = false;
	}
	/**
	 * 计算各个值的变动
	 * 
	 */
	public static void startCount() {
		  ss=ss+1;
		    if(ss>=60) {
		     ms = ms + 1;
		     ss=0;
		     }
			if(ms>=60) {
			     hs = hs+ 1;
			     ss=0;
			     ms = 0;
			     }
			if(hs>=24) {
			     dates = dates+1;
			     ss=0;
			     ms = 0;
			     hs = 0;  
			    }
			    if(dates>30) {
			     months = months +1;
			     ss=0;
			     ms = 0;
			     hs = 0; 
			     dates = 1;
			    }
			    if(months>12) {
			     years = years + 1;
			     ss=0;
			     ms = 0;
			     hs = 0; 
			     dates = 1;
			     months = 1;
			    }
		  
		//倒计时
		if (secondNotAlready) {
			if (second > 0) {
				second--;
				if (second == 0 && !minuteNotAlready) {
					secondNotAlready = false;
				}
			} else {
				if (minuteNotAlready) {
					if (minute > 0) {
						minute--;
						second = 59;
						if (minute == 0 && !hourNotAlready) {
							minuteNotAlready = false;
						}
					} else {
						if (hourNotAlready) {
							if (hour > 0) {
								hour--;
								minute = 59;
								second = 59;
								if (hour == 0 && !dayNotAlready) {
									hourNotAlready = false;
								}
							} else {
								if (dayNotAlready) {
									day--;
									hour = 23;
									minute = 59;
									second = 59;
									if (day == 0) {
										dayNotAlready = false;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	public static void main(String[] args) {
		
		long totalSeconds = 11;
		initData(totalSeconds);

		new Timer().schedule(new TimerTask() {
			public void run() {
				if (secondNotAlready) {
					startCount();
				} else {
					cancel();
				}
			}
		},0,1000);
		
	    time t1=new time();
	    while(true) {
		if(day==0&&minute==0&&second==0) {
			String aa=(""+years+"-"+months+"-"+dates+" "+hs+":"+ms+":"+ss+"    ");
			String bb=("香港在这一刻回归了！");
			t1.l1.setText(aa+bb);
			try { 
				URL cb; 
				File f = new File("E://0丶ME//java//one//src//one//sing.wav"); //引号里面的是音乐文件所在的绝对路径
				cb = f.toURL(); 
				AudioClip aau; 
				aau = Applet.newAudioClip(cb); 
				aau.play();
				//aau.loop();循环播放 aau.play() 单曲 aau.stop()停止播放  frame=new JavaClip(); 
				//frame.setBounds(0, 0, 300, 200); 
				//frame.setVisible(true); 
				} catch (MalformedURLException e) { 
				e.printStackTrace(); 
				} 
			break;
		}else {
			String aa=(""+years+"-"+months+"-"+dates+" "+hs+":"+ms+":"+ss+"    ");
			String cc=("距离香港回归还有：" + day + "天" + hour + "小时" + minute
					+ "分钟" + second + "秒");
			t1.l1.setText(aa+cc);
		}
	}
	}
}